<?php defined('SYSPATH') or die('No direct script access.');
 
class Controller_Home extends Controller_Common{
    
   public function action_index()
   {
          $this->network();
      //    $sidebar = View::factory('static/network');
      //    $this->template->sidebar = $sidebar;  
   //       $up = View::factory('proba');
   //       $this->template->up = $up;   
   //       $down = View::factory('proba');
  //        $this->template->down = $down;
       
           $blogs = ORM::factory('blog')->find_all();
           $content = View::factory('temp/temp2')
               ->bind('blogs', $blogs);
           $this->template->content = $content;  
       
       if(isset($_POST['det']))
       {   
            $a=$_POST["idb"];
            Cookie::set('temp', $a);  
            $this->redirect('Home/post');
       }
   }
    
    public function action_welcome()
    {
        //$this->network();
         $s = file_get_contents('http://ulogin.ru/token.php?token='.$_POST['token'].'&host='.$_SERVER['HTTP_HOST']);
         $user = json_decode($s, true); 
         Cookie::set('first_name', $user['first_name']);
         Cookie::set('last_name', $user['last_name']);
         Cookie::set('network', $user['network']);  
         Cookie::set('idenity', $user['identity']);      
         $content = View::factory('user/connect');
         $content->firstname = $user['first_name'];
         $content->lastname = $user['last_name'];
         $content->network = $user['network'];
         $this->template->content = $content;               
    }
    
    public function action_history()
    {
          $this->network();
          $content = View::factory('static/history');
          $this->template->content = $content; 
    }
    
    public function action_link()
    {
          $this->network();
          $content = View::factory('static/link');
          $this->template->content = $content;    
    }
    
    public function action_video()
    {     
          $this->network();
          $content = View::factory('user/video');
          $this->template->content = $content;
    }
    
    public function action_photo()
    {
           $this->network();
          $content = View::factory('user/photo');
          $this->template->content = $content;
    }
    
    public function action_post()
    {  
         $blogs = ORM::factory('blog')
                ->where( 'id', '=', Cookie::get('temp','no user'))
                ->find();
            if($blogs->loaded())
            {
                $data = array(
                    'id' => $blogs->id,
                    'title' => $blogs->title,
                    'text' => $blogs->text,
                    'views' => $blogs->views,
                    'date' => $blogs->date,
                    'time' => $blogs->time,
                    'photo1' => $blogs->photo->photo1,
                    'photo2' => $blogs->photo->photo2,
                    'photo3' => $blogs->photo->photo3,
                    'photo4' => $blogs->photo->photo4,
                    'photo5' => $blogs->photo->photo5,
                    'video' => $blogs->photo->photo5
               );          
            }
        
          $content = View::factory('post', $data);
          $this->template->content = $content;
    }
  
    public function cut_paragraph($string)
    {
        $your_desired_width = 100;
        $string = substr($string, 0, $your_desired_width+1);
        if (strlen($string) > $your_desired_width)
        {
            $string = wordwrap($string, $your_desired_width);
            $i = strpos($string, "\n");
            if ($i)
            {
                $string = substr($string, 0, $i);
            }
        }
        return $string;
    }
    
    
    public function network()    
    {
        if(isset($_COOKIE['last_name']) AND isset( $_COOKIE['first_name']) AND isset( $_COOKIE['network']))
        {
            $sidebar = View::factory('user/connect');
            $sidebar->firstname =Cookie::get('first_name', 'no user');
            $sidebar->lastname =Cookie::get('last_name', 'no user');
            $sidebar->network =Cookie::get('network', 'no user');
            $this->template->sidebar = $sidebar;     
            
            if(isset($_POST['exit']))
            {
                Cookie::delete('last_name');
                Cookie::delete('first_name');
                Cookie::delete('network');
                Cookie::delete('identity');
                $sidebar = View::factory('static/network');
                $this->template->sidebar = $sidebar; 
            }
        }
        else
        {
            $sidebar = View::factory('static/network');
            $this->template->sidebar = $sidebar;  
        }
    }   
} // End Home